<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;


/**
 * Class User
 * @package App\Models
 * @version August 28, 2017, 3:11 pm CST
 */
class User extends Authenticatable
{
    use  HasApiTokens, Notifiable, EntrustUserTrait;

    public $table = 'users';


    protected $dates = ['deleted_at'];


    protected $guarded = [];

    protected $hidden = ['password', 'remember_token'];


    public function getProvinceIdAttribute($value)
    {
        if (!empty($value)) {
            $city = new City();
//            return ['id' => $value, 'name' => $city->getShortName($value)];
            return $city->getShortName($value);
        } else {
            return '未知';
        }
    }

    public function getCityIdAttribute($value)
    {
        if (!empty($value)) {
            $city = new City();
//            return ['id' => $value, 'name' => $city->getShortName($value)];
            return $city->getShortName($value);
        } else {
            return '未知';
        }

    }

    public function getDistrictIdAttribute($value)
    {
        if (!empty($value)) {
            $city = new City();
//            return ['id' => $value, 'name' => $city->getShortName($value)];
            return $city->getShortName($value);
        } else {
            return '未知';
        }

    }

    public
    function getStatusAttribute($v)
    {
        $data = [1 => '正常', 2 => '禁用'];
        return $data[$v];
    }

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected
        $casts = [

    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public
    static $rules = [

    ];

    /**这里用来查找用户是否存在，这里先直接返回不进行验证
     * @param $userAccout
     * @return mixed
     */
    public function findForPassport($name)
    {
        return User::where(['name' => $name])->first();
    }

    public function validateForPassportPasswordGrant($password)
    {
        return md5($password) == $this->password;
    }


}
